/*
 * Author: Alberto De Pena
 * Applied Algorithms
 * cribbage.cpp
*/

#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

struct Card
{
    char suit;
    char type;
    int val;
    int sval;
};

int CardVal(char type)
{
    int val;
    switch (type)
    {
        case 'A': val = 1; break;
        case '2': val = 2; break;
        case '3': val = 3; break;
        case '4': val = 4; break;
        case '5': val = 5; break;
        case '6': val = 6; break;
        case '7': val = 7; break;
        case '8': val = 8; break;
        case '9': val = 9; break;
        case 'T': val = 10; break;
        case 'J': val = 10; break;
        case 'Q': val = 10; break;
        case 'K': val = 10; break;
        default: val = -50; break;
    }
    return val;
}

int StraightVal(char type)
{
    int val;
    switch (type)
    {
        case 'A': val = 1; break;
        case '2': val = 2; break;
        case '3': val = 3; break;
        case '4': val = 4; break;
        case '5': val = 5; break;
        case '6': val = 6; break;
        case '7': val = 7; break;
        case '8': val = 8; break;
        case '9': val = 9; break;
        case 'T': val = 10; break;
        case 'J': val = 11; break;
        case 'Q': val = 12; break;
        case 'K': val = 13; break;
        default: val = -50; break;
    }
    return val;
}

int PairPoints(char card1, char card2, char card3, char card4, char starter)
{
    int pairs = 0;

    if(card1 == card2)
        pairs++;
    if(card1 == card3)
        pairs++;
    if(card1 == card4)
        pairs++;
    if(card1 == starter)
        pairs++;
    if(card2 == card3)
        pairs++;
    if(card2 == card4)
        pairs++;
    if(card2 == starter)
        pairs++;
    if(card3 == card4)
        pairs++;
    if(card3 == starter)
        pairs++;
    if(card4 == starter)
        pairs++;

    return pairs * 2;
}

int SuitPoints(char card1, char card2, char card3, char card4, char starter)
{
    int suitPoints;
    if(card1 == card2 && card1 == card3 && card1 == card4)
    {
        if(card1 == starter)
            suitPoints = 5;
        else
            suitPoints = 4;
    }
    else
        suitPoints = 0;

    return suitPoints;
}

int JackPoints(Card card1, Card card2, Card card3, Card card4, char starter)
{
    int points = 0;
    if(card1.type == 'J')
    {
        if(card1.suit == starter)
            points = 1;
    }
    if(card2.type == 'J')
    {
        if(card2.suit == starter)
            points = 1;
    }
    if(card3.type == 'J')
    {
        if(card3.suit == starter)
            points = 1;
    }
    if(card4.type == 'J')
    {
        if(card4.suit == starter)
            points = 1;
    }
    return points;
}

int StraightPoints(int card1, int card2, int card3, int card4, int starter)
{
    priority_queue<int> PQ;
    int smax = 1, s = 1, last, points, start, cur, hand[5];

    PQ.push(card1);
    PQ.push(card2);
    PQ.push(card3);
    PQ.push(card4);
    PQ.push(starter);

    last = PQ.top();
    PQ.pop();
    start = 0;
    cur = 0;

    while(!PQ.empty())
    {
        cur++;
        if(PQ.top() == last -1)
        {
            start = s - cur;
            s++;
            if(s > smax)
                smax = s;
        }
        else
            s = 1;

        last = PQ.top();
        PQ.pop();
    }
    
    int mul = 1;
    PQ.push(card1);
    PQ.push(card2);
    PQ.push(card3);
    PQ.push(card4);
    PQ.push(starter);

    for(int i = 0; i < 5; i++)
    {
        hand[i] = PQ.top();
        PQ.pop();
    }

    if(smax >= 3)
    {
        if(smax == 3)
        {
            if(start == 0)
            {
                if(hand[2] == hand[3])
                {
                    mul++;
                    if(hand[2] == hand[4])
                        mul++;
                }
            }
            else if(start == 1)
            {
                if(hand[1] == hand[0])
                {
                    mul++;
                    if(hand[3] == hand[4])
                        mul *= 2;
                }
                else if(hand[3] == hand[4])
                    mul++;
            }
            else
            {
                if(hand[2] == hand[1])
                {
                    mul++;
                    if(hand[2] == hand[0])
                        mul++;
                }
            }
        }
        else if(smax == 4)
        {
            if(start == 0)
            {
                if(hand[3] == hand[4])
                    mul++;
            }
            else
            {
                if(hand[0] == hand[1])
                    mul++;
            }
        }

        points = smax * mul;
    }
    else
    {
        if(hand[0] == hand[2] + 1 && hand[0] == hand[4] + 2)
        {
            if(hand[1] == hand[2] && hand[2] == hand[3])
                points = 9;
            else if(hand[0] == hand[1] && hand[2] == hand[3])
                points = 12;
            else if(hand[1] == hand[2] && hand[3] == hand[4])
                points = 12;
        }
        else
            points = 0;
    }

    return points; 
}

int ComboPoints(int card1, int card2, int card3, int card4, int card5)
{
    int combos = 0;
    if(card1 + card2 == 15)
        combos++;
    if(card1 + card3 == 15)
        combos++;
    if(card1 + card4 == 15)
        combos++;
    if(card1 + card5 == 15)
        combos++;
    if(card2 + card3 == 15)
        combos++;
    if(card2 + card4 == 15)
        combos++;
    if(card2 + card5 == 15)
        combos++;
    if(card3 + card4 == 15)
        combos++;
    if(card3 + card5 == 15)
        combos++;
    if(card4 + card5 == 15)
        combos++;

    if(card1 + card2 + card3 == 15)
        combos++;
    if(card1 + card2 + card4 == 15)
        combos++;
    if(card1 + card2 + card5 == 15)
        combos++;
    if(card1 + card3 + card4 == 15)
        combos++;
    if(card1 + card3 + card5 == 15)
        combos++;
    if(card1 + card4 + card5 == 15)
        combos++;
    if(card2 + card3 + card4 == 15)
        combos++;
    if(card2 + card3 + card5 == 15)
        combos++;
    if(card2 + card4 + card5 == 15)
        combos++;
    if(card3 + card4 + card5 == 15)
        combos++;

    if(card1 + card2 + card3 + card4 == 15)
        combos++;
    if(card1 + card2 + card3 + card5 == 15)
        combos++;
    if(card1 + card2 + card4 + card5 == 15)
        combos++;
    if(card1 + card3 + card4 + card5 == 15)
        combos++;
    if(card2 + card3 + card4 + card5 == 15)
        combos++;

    if(card1 + card2 + card3 + card4 + card5 == 15)
        combos++;

    return combos * 2;
}

void main()
{
    ifstream fin;
    ofstream fout;

	fin.open("cribbage.in");
    fout.open("cribbage.out");

    int points;
    Card card1, card2, card3, card4, starter;

    fin >> card1.type;

    while(card1.type != '0')
    {
        points = 0;
        fin >> card1.suit;
        card1.val = CardVal(card1.type);
        card1.sval = StraightVal(card1.type);

        fin >> card2.type;
        fin >> card2.suit;
        card2.val = CardVal(card2.type);
        card2.sval = StraightVal(card2.type);

        fin >> card3.type;
        fin >> card3.suit;
        card3.val = CardVal(card3.type);
        card3.sval = StraightVal(card3.type);

        fin >> card4.type;
        fin >> card4.suit;
        card4.val = CardVal(card4.type);
        card4.sval = StraightVal(card4.type);

        fin >> starter.type;
        fin >> starter.suit;
        starter.val = CardVal(starter.type);
        starter.sval = StraightVal(starter.type);

        points += PairPoints(card1.type, card2.type, card3.type, card4.type, starter.type);
        points += SuitPoints(card1.suit, card2.suit, card3.suit, card4.suit, starter.suit);
        points += JackPoints(card1, card2, card3, card4, starter.suit);
        points += StraightPoints(card1.sval, card2.sval, card3.sval, card4.sval, starter.sval);
        points += ComboPoints(card1.val, card2.val, card3.val, card4.val, starter.val);

        fout << points << endl;
        

        fin >> card1.type;
    }

    fin.close();
    fout.close();
}

